package wootop.rabbitmq.transformation.configure;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class TopicRabbitConfig {

    static InetAddress address;

    static {
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }


    @Value("${wootop.rabbitMq.dept-routingKey}")
    private String deptRoutingKey;

    private static final Logger loger = LoggerFactory.getLogger(TopicRabbitConfig.class);


   @Bean
    public Queue deptQueue() {
        return new Queue("dept-queue" + address.getHostAddress(), false, true, true);
    }

    @Bean
    TopicExchange amqTopicExchange() {
        return new TopicExchange("amq.topic");
    }


    @Bean
    Binding bindingExchangeDeptQueue(Queue deptQueue) {
        return BindingBuilder.bind(deptQueue()).to(amqTopicExchange()).with(deptRoutingKey);
    }

//    /**
//     * 自动配置RabbitMQ 的连接工厂
//     */
//    @Autowired
//    private CachingConnectionFactory cachingConnectionFactory;
//

//    /**
//     *  单一消费者配置实例
//     * @return
//     */
//    @Bean(name = "singleListenerContainer")
//    public SimpleRabbitListenerContainerFactory listenerContainerFactory(){
//        //定义消息监听所在的容器工厂
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        /** 设置容器工厂所用的实例*/
//        factory.setConnectionFactory(cachingConnectionFactory);
//        /** 设置消息在传输中的格式，这里采用JSON 的格式进行传输*/
//        factory.setMessageConverter(new Jackson2JsonMessageConverter());
//        /** 设置并发消费者的初始化数量，这里设置为一个*/
//        factory.setConcurrentConsumers(1);
//        /** 设置消费者最大并发数量.,这里设置1个*/
//        factory.setMaxConcurrentConsumers(1);
//        /** 设置并发消费者实例中每个实例拉取的消息数量*/
//        factory.setPrefetchCount(1);
//        return factory;
//    }



//    public void sendFail(Message Message) {
//
////        rabbitTemplate.convertAndSend("amq.topic", "warming.user."+Message.getMessageProperties(), "用户已经下线！");
//    }

}
